package com.hmsg.health.service.admin;


import com.hmsg.health.bean.Dto.AppealDto;
import com.hmsg.health.bean.domain.AppealDo;
import com.hmsg.health.bean.domain.TaskDo;
import com.hmsg.health.bean.domain.TaskTypeDo;
import com.hmsg.health.bean.domain.UserAccountDo;
import com.hmsg.health.bean.domain.UserAccountRecordDo;
import com.hmsg.health.bean.domain.UserDo;
import com.hmsg.health.bean.domain.UserTaskDo;
import com.hmsg.health.dao.AppealDao;
import com.hmsg.health.dao.TaskDao;
import com.hmsg.health.dao.TaskTypeDao;
import com.hmsg.health.dao.UserAccountDao;
import com.hmsg.health.dao.UserAccountRecordDao;
import com.hmsg.health.dao.UserDao;
import com.hmsg.health.dao.UserTaskDao;
import com.hmsg.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdminAppealservice {

    @Autowired
    private AppealDao appealDao;
    @Autowired
    private UserTaskDao userTaskDao;
    @Autowired
    private TaskDao taskDao;
    @Autowired
    TaskTypeDao taskTypeDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserAccountDao userAccountDao;
    @Autowired
    private UserService userService;
    @Autowired
    private UserAccountRecordDao userAccountRecordDao;

    public List<AppealDto> findAll(Integer typeId,String phone,String time,
                                   Integer firstResult, Integer maxResult) {
        List<AppealDto> all = appealDao.findAll(typeId,phone,time,firstResult, maxResult);
        if(all != null){
            for(AppealDto a:all) {
                UserTaskDo one = userTaskDao.findOne(a.getUserTaskId());
                a.setTaskId(one.getTaskId());
                TaskDo one1 = taskDao.findOne(one.getTaskId());
                a.setTaskType(one1.getTypeDo().getName());
                UserDo user=userDao.findById(a.getUserId());//申诉人id
                a.setAppealName(user.getUserName());
                a.setPhone(user.getMobile());

                UserDo createUser=userDao.findById(one1.getUser().getId());
                a.setCreateUserName(createUser.getUserName());
                a.setCreateUserPhone(createUser.getMobile());
                a.setNpassImg(one.getNopassImg());
                a.setIsState(one1.getTaskStatus());

                a.setNpassReason(one.getNpassReason());
                a.setVerifyImg(one.getVerifyDetail());
            }
        }
        return all;
    }

    @Transactional
    public void update(AppealDo appealDo) {
        AppealDo one1 = appealDao.findOne(appealDo.getId());

        Integer update = appealDao.update(appealDo);
        AppealDo one2 = appealDao.findOne(appealDo.getId());

        if(one2.getStatus() == 1) {
            TaskDo one = taskDao.findOne(one1.getTaskId());
            Integer taskGold = one.getTaskGold();//金币

            UserAccountDo userAccount = userAccountDao.findUserAccount(one1.getUserId());
            if(userAccount != null) {
                userAccountDao.updateByUserId(one1.getUserId(), taskGold);
            }else{
                UserAccountDo ua = new UserAccountDo();
                ua.setGlod(taskGold);
                ua.setCanWithdraw(0);
                ua.setScore(0);
                ua.setUserId(one1.getUserId());
                userService.saveUserAccount(ua);
            }
            UserAccountDo createAccount = userAccountDao.findUserAccount(one.getUserId());
            if(createAccount != null){
                userAccountDao.updateTaskGold(one.getUserId(), taskGold);
            }

            //申诉者的账号变动
            UserAccountRecordDo userAccountRecordDo=new UserAccountRecordDo();
            userAccountRecordDo.setAmount(one.getTaskGold());
            userAccountRecordDo.setUserId(appealDo.getUserId());
            userAccountRecordDo.setGlodType(1);
            userAccountRecordDo.setType(7);
            userAccountRecordDo.setCreateTime(new Date());

            userAccountRecordDo.setTitle("申诉退回");
            userAccountRecordDao.save(userAccountRecordDo);



            //发起者的账号变动
            UserAccountRecordDo userAccountRecordDo1=new UserAccountRecordDo();
            userAccountRecordDo1.setAmount(one.getTaskGold());
            userAccountRecordDo1.setUserId(one.getUserId());
            userAccountRecordDo1.setGlodType(2);
            userAccountRecordDo1.setType(8);
            userAccountRecordDo1.setCreateTime(new Date());

            userAccountRecordDo.setTitle("申诉扣除");
            userAccountRecordDao.save(userAccountRecordDo);

        }
    }
}
